package com.lzb.findphone.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.findphone.BaseNoModelFragment;
import com.lzb.findphone.R;
import com.lzb.findphone.adapter.RingtoneAdapter;
import com.lzb.findphone.basic.media.AudioPlayerImpl;
import com.lzb.findphone.bean.SettingBean;
import com.lzb.findphone.bean.SoundBean;
import com.lzb.findphone.databinding.FragmentRingtoneBinding;
import com.lzb.findphone.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RingtoneFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RingtoneFragment extends BaseNoModelFragment<FragmentRingtoneBinding> {

    private RingtoneAdapter ringtoneAdapter;
    private List<SoundBean> listShow = new ArrayList<>();
    private AudioPlayerImpl mAudioPlayer;
    private AudioPlayerImpl mCurAudioPlayer;


    @Override
    protected int onCreate() {
        return R.layout.fragment_ringtone;
    }

    @Override
    protected void initView() {
        mAudioPlayer = new AudioPlayerImpl();
        mCurAudioPlayer = new AudioPlayerImpl();

        mDataBinding.rvRingtone.setLayoutManager(new GridLayoutManager(getContext(), 2));
        ringtoneAdapter = new RingtoneAdapter(mAudioPlayer);
        ringtoneAdapter.mCurAudioPlayer = mCurAudioPlayer;
        mDataBinding.rvRingtone.setAdapter(ringtoneAdapter);
        ringtoneAdapter.setOnItemClickListener(new RingtoneAdapter.OnItemClickListener() {
            @Override
            public void onItemApply(int position) {
                SettingBean settingBean = AppSpUtils.getSetting();
                settingBean.setRing(listShow.get(position).getRawId());
                AppSpUtils.saveSetting(settingBean);
                ToastUtils.showShort(getString(R.string.apply_suc));
            }
        });
    }

    @Override
    protected void initData() {
        listShow.add(new SoundBean(R.drawable.icon_dog3, R.drawable.icon_dog2, "狗", R.raw.gou, false));
        listShow.add(new SoundBean(R.drawable.icon_police3, R.drawable.icon_police2, "警察", R.raw.jingcha, false));
        listShow.add(new SoundBean(R.drawable.icon_cattle3, R.drawable.icon_cattle2, "牛", R.raw.niu, false));
        listShow.add(new SoundBean(R.drawable.icon_sheep3, R.drawable.icon_sheep2, "羊", R.raw.yang, false));
        listShow.add(new SoundBean(R.drawable.icon_frog3, R.drawable.icon_frog2, "青蛙", R.raw.qingwa, false));
        listShow.add(new SoundBean(R.drawable.icon_pig3, R.drawable.icon_pig2, "猪", R.raw.zhu, false));
        listShow.add(new SoundBean(R.drawable.icon_goast3, R.drawable.icon_goast2, "鬼", R.raw.gui, false));
        listShow.add(new SoundBean(R.drawable.icon_baby3, R.drawable.icon_baby2, "婴儿", R.raw.yinger, false));
        listShow.add(new SoundBean(R.drawable.icon_car3, R.drawable.icon_car2, "卡车", R.raw.kache, false));
        listShow.add(new SoundBean(R.drawable.icon_police_car3, R.drawable.icon_police_car2, "警车", R.raw.jingche, false));
        listShow.add(new SoundBean(R.drawable.icon_laba3, R.drawable.icon_laba2, "喇叭", R.raw.laba, false));
        listShow.add(new SoundBean(R.drawable.icon_lightning3, R.drawable.icon_lightning2, "闪电", R.raw.shandian, false));
        listShow.add(new SoundBean(R.drawable.icon_clock3, R.drawable.icon_clock2, "闹钟", R.raw.naozhong, false));
        ringtoneAdapter.setSoundBeans(listShow);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            if (mAudioPlayer != null) {
                mAudioPlayer.pause();
            }

            if (mCurAudioPlayer != null) {
                mCurAudioPlayer.pause();
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAudioPlayer != null) {
            mAudioPlayer.pause();
        }

        if (mCurAudioPlayer != null) {
            mCurAudioPlayer.pause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mAudioPlayer != null) {
            mAudioPlayer.stop();
        }

        if (mCurAudioPlayer != null) {
            mCurAudioPlayer.stop();
        }
    }
}